package manager.cms.service.exec;

import common.SnowflakeIdWorker;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.JdExecutor;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import manager.feign.ShopIndexFloorThirdService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取优惠卷信息
 */
@Service(value = "activityExec")
public class ActivityExecImpl implements JdExecutor {
    @Autowired
    private ShopIndexFloorThirdService shopIndexFloorThirdService;
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    @Autowired
    private CmsContentDao cmsContenDao;
    @Override
    public void execute(Map<String,Object> mapParams) {
        try {
            JSONObject activityJsonObject = null;
            CmsContentCatVO cmsContentCatVO=new CmsContentCatVO();
            String  activity=shopIndexFloorThirdService.getActivity();
            String activityStr=activity.substring(14,activity.length()-1);
            activityJsonObject = new JSONObject(activityStr);
            JSONObject jsonData=(JSONObject)activityJsonObject.get("data");
            String activityDataId=String.valueOf(SnowflakeIdWorker.nextId());
            String shopId=String.valueOf(SnowflakeIdWorker.nextId());
            cmsContentCatVO.setName("活动专区-->活动商品");
            cmsContentCatVO.setId(activityDataId);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            cmsContentCatVO.setName("活动专区-->活动店");
            cmsContentCatVO.setId(shopId);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            String backCateId=String.valueOf(SnowflakeIdWorker.nextId());
            String backId=String.valueOf(SnowflakeIdWorker.nextId());
            cmsContentCatVO.setName("活动专区-->背景图片");
            cmsContentCatVO.setId(backCateId);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            JSONArray activityData=(JSONArray)jsonData.get("act");
            List<CmsContentVO> contentVOList=new ArrayList<CmsContentVO>();
            CmsContentVO cmsContent=new CmsContentVO();
            cmsContent.setPic(jsonData.get("benefitImgW").toString());
            cmsContent.setId(backId);
            cmsContent.setCategoryId(backCateId);
            contentVOList.add(cmsContent);
            for(int i=0;i<activityData.length();i++){
                CmsContentVO cmsContentVO=new CmsContentVO();
                String id=String.valueOf(SnowflakeIdWorker.nextId());
                JSONObject item=(JSONObject)activityData.get(i);
                cmsContentVO.setPic("//img12.360buyimg.com/img/s260x260_"+item.get("imgUrl").toString());
                cmsContentVO.setId(id);
                cmsContentVO.setTitle(item.get("name").toString()+";"+item.get("desc").toString());
                cmsContentVO.setCategoryId(activityDataId);
                contentVOList.add(cmsContentVO);
            }
            cmsContenDao.addCmsContentBatch(contentVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
